package com.shanzhu.book.web;

import com.shanzhu.book.model.SeatReservation;
import com.shanzhu.book.service.SeatReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api22/seat")
public class SeatReservationController2 {

    @Autowired
    private SeatReservationService seatReservationService;

    @PostMapping("/reserve")
    public ResponseEntity<?> reserveSeat(@RequestBody SeatReservation reservation) {
        try {
            boolean success = seatReservationService.reserveSeat(reservation);
            return success ? ResponseEntity.ok().build() : 
                           ResponseEntity.badRequest().body("Seat not available");
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(e.getMessage());
        }
    }

    @PostMapping("/cancel/{id}")
    public ResponseEntity<?> cancelReservation(@PathVariable Long id, 
                                              @RequestParam Long userId) {
        boolean success = seatReservationService.cancelReservation(id, userId);
        return success ? ResponseEntity.ok().build() : 
                       ResponseEntity.badRequest().body("Cannot cancel reservation");
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<SeatReservation>> getUserReservations(@PathVariable Long userId) {
        return ResponseEntity.ok(seatReservationService.getUserReservations(userId));
    }

    @GetMapping("/current/{userId}")
    public ResponseEntity<SeatReservation> getCurrentReservation(@PathVariable Long userId) {
        SeatReservation reservation = seatReservationService.getCurrentReservation(userId);
        return reservation != null ? ResponseEntity.ok(reservation) : 
                                    ResponseEntity.notFound().build();
    }

//    @GetMapping("/search")
//    public ResponseEntity<PageInfo<SeatReservation>> searchReservations(
//            @RequestParam(required = false) String seatNumber,
//            @RequestParam(required = false) String location,
//            @RequestParam(required = false) Integer status,
//            @RequestParam(defaultValue = "1") int pageNum,
//            @RequestParam(defaultValue = "10") int pageSize) {
//        return ResponseEntity.ok(seatReservationService.searchReservations(
//            seatNumber, location, status, pageNum, pageSize));
//    }

    @PostMapping("/checkin/{id}")
    public ResponseEntity<?> checkIn(@PathVariable Long id, 
                                    @RequestParam Long userId) {
        boolean success = seatReservationService.checkIn(id, userId);
        return success ? ResponseEntity.ok().build() : 
                       ResponseEntity.badRequest().body("Check-in failed");
    }

    @PostMapping("/checkout/{id}")
    public ResponseEntity<?> checkOut(@PathVariable Long id, 
                                     @RequestParam Long userId) {
        boolean success = seatReservationService.checkOut(id, userId);
        return success ? ResponseEntity.ok().build() : 
                       ResponseEntity.badRequest().body("Check-out failed");
    }
}